package com.nbicc.service.modular.dp.widgetinstancevalue.service;

import com.nbicc.core.db.modular.dp.widgetinstancevalue.imodel.AWidgetInstanceValue;
import com.nbicc.core.service.modular.dp.widgetinstancevalue.IWidgetInstanceValueService;
import com.nbicc.core.utils.ToolUtils;
import com.nbicc.db.modular.dp.widgetinstancevalue.mapper.WidgetInstanceBigvalueMapper;
import com.nbicc.db.modular.dp.widgetinstancevalue.mapper.WidgetInstanceValueMapper;
import com.nbicc.db.modular.dp.widgetinstancevalue.model.WidgetInstanceBigvalue;
import com.nbicc.db.modular.dp.widgetinstancevalue.model.WidgetInstanceValue;
import com.nbicc.db.modular.dp.widgetinstancevalue.model.WidgetInstanceValueImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lilingyan
 */
@Service("MysqlWidgetInstanceValueService")
public class MysqlWidgetInstanceValueService implements IWidgetInstanceValueService {

    @Autowired
    private WidgetInstanceValueMapper widgetInstanceValueMapper;
    @Autowired
    private WidgetInstanceBigvalueMapper widgetInstanceBigvalueMapper;

    @Override
    public List<AWidgetInstanceValue> listByWidgetInstanceId(Integer widgetInstanceId) {
        List<WidgetInstanceValue> widgetInstanceValueList = widgetInstanceValueMapper.selectByWidgetInstanceId(widgetInstanceId);
        List<WidgetInstanceBigvalue> widgetInstanceBigvalueList = widgetInstanceBigvalueMapper.selectByWidgetInstanceId(widgetInstanceId);

        List<AWidgetInstanceValue> aWidgetInstanceValueArrayList = new ArrayList<>();
        aWidgetInstanceValueArrayList.addAll(widgetInstanceValueList);
        aWidgetInstanceValueArrayList.addAll(widgetInstanceBigvalueList);
        return aWidgetInstanceValueArrayList;
    }

    @Transactional
    @Override
    public Integer setAllWidgetInstanceValue(Integer widgetInstanceId , List<? extends AWidgetInstanceValue> aWidgetInstanceValueList) {

        List<WidgetInstanceValue> widgetInstanceValueList = new ArrayList<>();
        List<WidgetInstanceBigvalue> widgetInstanceBigvalueList = new ArrayList<>();

        if(aWidgetInstanceValueList != null){
            for (AWidgetInstanceValue aWidgetInstanceValue : aWidgetInstanceValueList) {
                WidgetInstanceValueImpl widgetInstanceValueImpl = (WidgetInstanceValueImpl) aWidgetInstanceValue;
                widgetInstanceValueImpl.setWidgetInstanceId(widgetInstanceId);
                if(widgetInstanceValueImpl.getWebPointIsBigvalue()){
                    WidgetInstanceBigvalue widgetInstanceBigvalue = new WidgetInstanceBigvalue();
                    BeanUtils.copyProperties(widgetInstanceValueImpl , widgetInstanceBigvalue);
                    widgetInstanceBigvalueList.add(widgetInstanceBigvalue);
                }else{
                    WidgetInstanceValue widgetInstanceValue = new WidgetInstanceValue();
                    BeanUtils.copyProperties(widgetInstanceValueImpl , widgetInstanceValue);
                    widgetInstanceValueList.add(widgetInstanceValue);
                }
            }
        }

        Integer affects = deleteByWidgetInstanceId(widgetInstanceId);
        affects += insertAllWidgetInstanceValueList(widgetInstanceValueList);
        affects += insertAllWidgetInstanceBigvalueList(widgetInstanceBigvalueList);
        return affects;
    }

    @Transactional
    public Integer insertAllWidgetInstanceValueList(List<WidgetInstanceValue> widgetInstanceValueList) {
        if(widgetInstanceValueList != null && widgetInstanceValueList.size() > 0){
            for (WidgetInstanceValue widgetInstanceValue : widgetInstanceValueList) {
                String widgetInstanceId = ToolUtils.generateUUID32();
                widgetInstanceValue.setId(widgetInstanceId);
            }
            return widgetInstanceValueMapper.insertAll(widgetInstanceValueList);
        }
        return 0;
    }

    @Transactional
    public Integer insertAllWidgetInstanceBigvalueList(List<WidgetInstanceBigvalue> widgetInstanceBigvalueList) {
        if(widgetInstanceBigvalueList != null && widgetInstanceBigvalueList.size() > 0){
            for (WidgetInstanceBigvalue widgetInstanceBigvalue : widgetInstanceBigvalueList) {
                String widgetInstanceId = ToolUtils.generateUUID32();
                widgetInstanceBigvalue.setId(widgetInstanceId);
            }
            widgetInstanceBigvalueMapper.insertAll(widgetInstanceBigvalueList);
        }
        return 0;
    }

    @Transactional
    @Override
    public Integer updateAllWidgetInstanceValueById(Integer widgetInstanceId, List<? extends AWidgetInstanceValue> aWidgetInstanceValueList) {
        if(aWidgetInstanceValueList != null){
            for (AWidgetInstanceValue aWidgetInstanceValue : aWidgetInstanceValueList) {
                WidgetInstanceValueImpl widgetInstanceValueImpl = (WidgetInstanceValueImpl) aWidgetInstanceValue;
                widgetInstanceValueImpl.setWidgetInstanceId(widgetInstanceId);
                if(widgetInstanceValueImpl.getWebPointIsBigvalue()){
                    WidgetInstanceBigvalue widgetInstanceBigvalue = new WidgetInstanceBigvalue();
                    BeanUtils.copyProperties(widgetInstanceValueImpl , widgetInstanceBigvalue);
                    widgetInstanceBigvalueMapper.updateById(widgetInstanceBigvalue);
                }else{
                    WidgetInstanceValue widgetInstanceValue = new WidgetInstanceValue();
                    BeanUtils.copyProperties(widgetInstanceValueImpl , widgetInstanceValue);
                    widgetInstanceValueMapper.updateById(widgetInstanceValue);
                }
            }
        }
        return null;
    }

    @Transactional
    @Override
    public Integer deleteByWidgetInstanceId(Integer widgetInstanceId) {
        Integer affects = widgetInstanceValueMapper.deleteByWidgetInstanceId(widgetInstanceId);
        affects += widgetInstanceBigvalueMapper.deleteByWidgetInstanceId(widgetInstanceId);
        return affects;
    }

}
